//
//  BiPurchaseListViewController.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/4/27.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiPurchaseListViewController.h"
#import "MJRefresh.h"

#import "PurchaseListCell.h"
#import "StorageListCell.h"

#import "BiPurchaseYanshouEntity.h"
#import "BiPurchaseCheckEntity.h"
#import "BiPurchaseRefitEntity.h"
#import "BiPurChaseCarInfoEntity.h"

#import "BiPurchaseOrderCheckViewController.h"
#import "BiPurchaseRefitCheckViewController.h"
#import "BiPurchaseYanshouViewController.h"
@interface BiPurchaseListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView * _tableView;
    
    NSMutableArray * _purchaseExecuteList;
    
}
@end

@implementation BiPurchaseListViewController

- (id)init
{
    self = [super init];
    if (self)
    {
        _purchaseExecuteList = [[NSMutableArray alloc] init];
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBackBtn];
    
    
    if (0 == self.type) {
        
        [self setNavBarTitle:@"订单审批列表"];
    }else if (1 == self.type){
        
        [self setNavBarTitle:@"改装上线列表"];
    }else if (2 == self.type){
        
        [self setNavBarTitle:@"验车入库列表"];
    }
    
    [self setNavBarWithHidden:NO];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, [self viewTop], MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT-[self viewTop])];
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    
    _tableView.rowHeight = 72;
    
    if (2 == self.type) {
        
        _tableView.rowHeight = 80;
    }
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setupRefresh];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId",
                              @"0",@"page",
                              PAGESIZE,@"pageSize",
                              nil];
    
    if (0 == self.type){
        
        [[BiNetEngine shareInstance] requestPurchaseGetOrderCheckList:self paraDict:paraDic];
        
        
    }else if (1 == self.type){
        
        [[BiNetEngine shareInstance] requestPurchaseGetOrderRefitList:self paraDict:paraDic];
        
    }else if (2 == self.type){
    
        [[BiNetEngine shareInstance] requestPurchaseGetOrderYanshouList:self paraDict:paraDic];
    }
    
    [self asyshowHUDView:WAITING CurrentView:self];
}


/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    //    [_tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    _tableView.headerPullToRefreshText = @"下拉刷新";
    _tableView.headerReleaseToRefreshText = @"松开即可刷新";
    _tableView.headerRefreshingText = @"刷新中~";
    
    _tableView.footerPullToRefreshText = @"上拉加载更多数据";
    _tableView.footerReleaseToRefreshText = @"松开马上加载";
    _tableView.footerRefreshingText = @"加载中~";
}


#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSLog(@"headerRereshing");
    
    
    NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId",
                              @"0",@"page",
                              PAGESIZE,@"pageSize",
                              nil];
    
    if (0 == self.type){
        
        [[BiNetEngine shareInstance] requestPurchaseGetOrderCheckList:self paraDict:paraDic];
        
        
    }else if (1 == self.type){
        
        [[BiNetEngine shareInstance] requestPurchaseGetOrderRefitList:self paraDict:paraDic];
        
    }else if (2 == self.type){
        
        [[BiNetEngine shareInstance] requestPurchaseGetOrderYanshouList:self paraDict:paraDic];
    }
    
   // [self performSelector:@selector(fffff) withObject:nil afterDelay:2];
}

- (void)fffff{
    
    [_tableView headerEndRefreshing];
    [_tableView footerEndRefreshing];
    
}

- (void)footerRereshing
{
    
    [self performSelector:@selector(fffff) withObject:nil afterDelay:2];
    NSLog(@"footerRereshing");
}


#pragma -mark UitabeViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (self.type) {
        case 0:
        {
            BiPurchaseCheckEntity * cEntity = _purchaseExecuteList[indexPath.row];
            BiPurchaseOrderCheckViewController * cVtrcl = [[BiPurchaseOrderCheckViewController alloc] init];
            cVtrcl.centity = cEntity;
            [self.navigationController pushViewController:cVtrcl animated:YES];
        }
            break;
        case 1:
        {
            BiPurchaseRefitEntity * rEntity = _purchaseExecuteList[indexPath.row];
            BiPurchaseRefitCheckViewController * cVtrcl = [[BiPurchaseRefitCheckViewController alloc] init];
            cVtrcl.centity = rEntity;
            [self.navigationController pushViewController:cVtrcl animated:YES];
        }
            break;
        case 2:
        {
            BiPurchaseYanshouEntity * yEntity = _purchaseExecuteList[indexPath.row];
            BiPurchaseYanshouViewController * cVtrcl = [[BiPurchaseYanshouViewController alloc] init];
            cVtrcl.centity = yEntity;
            [self.navigationController pushViewController:cVtrcl animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma -mark UitabeViewDatasource

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_purchaseExecuteList) {
        
        return _purchaseExecuteList.count;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (2 == self.type) {
        
        StorageListCell * cell = (StorageListCell *)[self cellForStorageRowAtIndexPath:indexPath];
        
        return cell;
    }else{
    
    
        PurchaseListCell * cell = (PurchaseListCell *)[self cellForPurchaseRowAtIndexPath:indexPath];
        
        return cell;
    }
    
    return nil;
}

- (StorageListCell *)cellForStorageRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * myIndentifier = @"StorageListCell";
    
    StorageListCell * cell = (StorageListCell *)[_tableView dequeueReusableCellWithIdentifier:myIndentifier];
    
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"StorageListCell" owner:self options:nil] firstObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    BiPurchaseYanshouEntity * entity = _purchaseExecuteList[indexPath.row];
    BiPurChaseCarInfoEntity * carentity = entity.carEntity;
    cell.labBrand.text = carentity.brand;
    cell.labCarSeries.text = carentity.carSeries;
    cell.labDriver.text = carentity.carDrive;
    
    return cell;
    
}

- (PurchaseListCell *)cellForPurchaseRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * myIndentifier = @"PurchaseListCell";
    
    PurchaseListCell * cell = (PurchaseListCell *)[_tableView dequeueReusableCellWithIdentifier:myIndentifier];
    
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PurchaseListCell" owner:self options:nil] firstObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    BiPurChaseCarInfoEntity * carentity = nil;
    if (1 == self.type ) {
        
        BiPurchaseRefitEntity * entity = _purchaseExecuteList[indexPath.row];
        carentity = entity.carEntity;
    }else if (0 == self.type){
    
        BiPurchaseCheckEntity * entity = _purchaseExecuteList[indexPath.row];
        carentity = entity.carEntity;
    }
    
    cell._labBrand.text  = carentity.brand;
    cell._labCarSeries.text = carentity.carSeries;
    cell._labDrvRoom.text = carentity.cabType;
    cell._labNotice.text = carentity.announcementModel;
    return cell;
    
}

#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    //[self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    [_tableView headerEndRefreshing];
    [_tableView footerEndRefreshing];
    
    [_purchaseExecuteList removeAllObjects];
    
    [_purchaseExecuteList addObjectsFromArray:entity_[@"data"]];
    
    [_tableView reloadData];
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
}


@end
